﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Rabbit.API.Application.Command.Breed;

namespace Rabbit.API.Application.Handler.Breed
{
    public class BreedUpdateHandler:IRequestHandler<BreedUpdateCommand ,APIResult<int>>
    {
        private readonly IRepository<BreedModel> breedway;
        private readonly IMapper mapper;
        private readonly ILogger<BreedUpdateHandler> log;

        public BreedUpdateHandler(IRepository<BreedModel> breedway,IMapper mapper,ILogger<BreedUpdateHandler> log)
        {
            this.breedway = breedway;
            this.mapper = mapper;
            this.log = log;
        }
        /// <summary>
        /// 修改兔子养殖信息
        /// </summary>
        /// <param name="request">参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<int>> Handle(BreedUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res = new APIResult<int>();
            try
            {
            
                var value = mapper.Map<BreedModel>(request);
        
                var result = await breedway.Updateway(value);
                if (result > 0)
                {
                    log.LogInformation($"养殖信息修改成功");
                    res.Message = "养殖信息修改成功";
                    res.Num = StateCode.OK;
                    res.Data = result;

                }
                else
                {
                    res.Message = "养殖信息修改失败";
                    res.Num = StateCode.Fail;
                }
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
